package com.fll.fangyishare.service.impl;

import com.fll.fangyicommon.entity.share.Collect;
import com.fll.fangyicommon.entity.share.Comment;
import com.fll.fangyishare.dao.CollectDao;
import com.fll.fangyishare.dao.CommentDao;
import com.fll.fangyishare.service.CollectService;
import com.fll.fangyishare.service.CommentLikedService;
import com.fll.fangyishare.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author fll
 * create 2023/3/26
 */
@Service
@Slf4j
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectDao collectDao;

    /**
     * 获取当前登录用户的收藏的share
     * 先查出点赞过的share_id，然后再去查share即可
     * @param userId
     * @return
     */
    @Override
    public String getCollectByUser(Integer userId){
        ArrayList<Integer> shareIds = collectDao.getCollectByUser(userId);
        if(shareIds != null || shareIds.size() != 0){
            StringBuilder sb = new StringBuilder("(");
            for(Integer id : shareIds){
                sb.append(id);
                sb.append(",");
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append(")");
            return sb.toString();
        }
        return null;
    }

    /**
     * 判断是否已评论
     * @param userId
     * @param shareId
     * @return
     */
    @Override
    public Boolean isCollect(Integer userId, Integer shareId) {
        Collect collect = collectDao.isCollect(userId, shareId);
        if (collect != null){
            log.info("用户===>{},已收藏过share===>{}",userId, shareId);
            return true;
        }
        log.warn("用户===>{},未收藏share===>{}",userId, shareId);
        return false;
    }

    /**
     * 添加一条收藏记录
     * @param collect
     * @return
     */
    @Override
    public Integer addCollect(Collect collect){
        Integer num = collectDao.addCollect(collect);
        if (num != 0){
            log.info("收藏成功,用户===>{},share===>{}", collect.getUserId(), collect.getShareId());
            return num;
        }
        log.warn("收藏失败");
        return -1;
    }

    /**
     * 取消某条share的收藏
     * @param collect
     * @return
     */
    @Override
    public Integer cancelCollect(Collect collect){
        Integer num = collectDao.cancelCollect(collect);
        if (num != 0){
            log.info("取消收藏成功,用户===>{},share===>{}", collect.getUserId(), collect.getShareId());
            return num;
        }
        log.warn("取消收藏失败");
        return -1;
    }

    /**
     * 某条share总收藏数
     * @param shareId
     * @return
     */
    @Override
    public Integer getCollectCount(Integer shareId){
        Integer num = collectDao.getCollectCount(shareId);
        log.info("share===>{},被收藏的总数为===>{}", shareId, num);
        return num;
    }

    /**
     * 获取当前登录用户收藏的所有的share
     * @param userId
     * @return
     */
    @Override
    public ArrayList<Collect> getUserAllCollect(Integer userId){
        ArrayList<Collect> collectList = collectDao.getUserAllCollect(userId);
        if (collectList != null || collectList.size() != 0){
            log.info("用户===>{},收藏的share如下：===>{}", userId, collectList);
            return collectList;
        }
        log.warn("用户===>{},收藏的share为空：===>{}", userId, collectList);
        return null;
    }

}
